<?php
/**
*	模块信息
*/
namespace Rainrock\Framework\kernel\core;


use Rainrock\Framework\kernel\base\Rock;
use Rainrock\Framework\kernel\base\CLog;
use Rainrock\Framework\kernel\db\DB;

class ModuleInfo{
	
	use ModuleInfo_Update;
	
	private $num = '',$table = '',$name = '',$icons = '',$tablename='',$group='',$data = array();
	
	private $fieldsArray = array();
	private $indexArray  = array();
	
	protected function initModule(){}
	protected function initFirst(){}
	protected $nDb,$model;
	
	public $mListener	= null;
	public $mid			= 0;
	
	/**
	*	是否开启录入
	*/
	protected $isinput	= true;
	
	public function __construct()
	{
		$this->initModule();
	}
	
	/**
	*	获取监听事件
	*/
	public function getListener()
	{
		$this->mListener = Module::getListener($this->num, $this->group);
		$this->mListener->setMode($this);
		$this->mListener->initListener();
		return $this->mListener;
	}
	
	/**
	*	是否录入
	*/
	public function setIsinput($str)
	{
		$this->isinput = $str;
		return $this;
	}
	
	/**
	*	是否录入
	*/
	public function getIsinput()
	{
		return $this->isinput;
	}
	
	/**
	*	获取模块名
	*/
	public function getName()
	{
		return $this->name;
	}
	
	/**
	*	获取模块名
	*/
	public function getNum()
	{
		return $this->num;
	}
	
	/**
	*	获取模块名
	*/
	public function getGroup()
	{
		return $this->group;
	}
	
	/**
	*	获取全表名
	*/
	public function getTablename()
	{
		return $this->tablename;
	}
	
	/**
	*	设置编号
	*/
	public function setNum($num)
	{
		$this->num = $num;
		$this->setTable($num);
		return $this;
	}
	
	/**
	*	设置组编号
	*/
	public function setGroup($group)
	{
		$this->group = $group;
		return $this;
	}
	
	/**
	*	设置对应表
	*/
	public function setTable($table)
	{
		$this->table 		= $table;
		return $this;
	}
	
	/**
	*	获取对应表名
	*/
	public function getTable()
	{
		return $this->table;
	}
	
	/**
	*	设置模块名
	*/
	public function setName($name)
	{
		$this->name = $name;
		return $this;
	}
	
	/**
	*	设置图标
	*/
	public function setIcons($icons)
	{
		$this->icons = $icons;
		return $this;
	}
	
	/**
	*	添加索引
	* @param $type key/only/jian
	*/
	public function addIndex($type, $fields, $name='')
	{
		if(!$name)$name = $fields;
		$this->indexArray[] = array(
			'type' 	 => $type,
			'fields' => $fields,
			'name' 	 => $name,
		);
	}
	
	/**
	*	加字段
	*/
	public function addFields($obj)
	{
		$this->fieldsArray[] = $obj->getFields();
	}
	
	/**
	*	设置字段
	*/
	public function setFields($fields, $obj)
	{
		$xu = -1;
		foreach($this->fieldsArray as $k=>$rs){
			if($rs['fields'] == $fields){
				$xu = $k;
				break;
			}
		}
		if($xu > -1){
			$barr = $obj->getFields();
			foreach($barr as $k=>$v)$this->fieldsArray[$xu][$k] = $v;
		}else{
			$obj->setFields($fields);
			$this->addFields($obj);
		}
	}
	
	/**
	*	获取字段
	*/
	public function getFields()
	{
		return $this->fieldsArray;
	}
	
	/**
	*	获取模块信息
	*/
	public function getInfo()
	{
		return array(
			'num' 	=> $this->num,
			'table' => $this->table,
			'name' 	=> $this->name,
			'icons' => $this->icons,
		);
	}
	
	/**
	*	获取模型
	*/
	public function getModel($db=null)
	{
		if($this->model != null)return $this->model;
		$this->model = Model::get($this->table, $db);
		return $this->model;
	}
	
	/**
	*	加载当前数据
	*/
	public function loadData($mid)
	{
		$this->mid = $mid;
		$this->mListener->mid = $mid;
	}
}